<?php 
include('../../../../inc/application_settings.php');

$email = $_COOKIE['EMAIL'];
$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);
$response = array();

if ($auth->checkAuthentication()){
	switch ($target) {
		case 'NEW':
			$response = createNewNews();
			break;
		case 'EDIT':
			$response = updateNews();
			break;
	}
}

echo json_encode($response);


/**
 * updates the news
 */
function updateNews(){
	$response = array('SUCCESS' => 1, 'SUCCESS_MSG' => 'News erfolgreich upgedated!', 'NEWS_ID' => null);
	$headline_de = (isset($_REQUEST['NEWS_HEADLINE_DE'])) ? $_REQUEST['NEWS_HEADLINE_DE'] : null;
	$headline_en = (isset($_REQUEST['NEWS_HEADLINE_EN'])) ? $_REQUEST['NEWS_HEADLINE_EN'] : null;
	$shortinfo_de = (isset($_REQUEST['NEWS_SHORT_INFO_DE'])) ? $_REQUEST['NEWS_SHORT_INFO_DE'] : null;
	$shortinfo_en = (isset($_REQUEST['NEWS_SHORT_INFO_EN'])) ? $_REQUEST['NEWS_SHORT_INFO_EN'] : null;
	$longinfo_de = (isset($_REQUEST['NEWS_LONG_INFO_DE'])) ? $_REQUEST['NEWS_LONG_INFO_DE'] : null;
	$longinfo_en = (isset($_REQUEST['NEWS_LONG_INFO_EN'])) ? $_REQUEST['NEWS_LONG_INFO_EN'] : null;
	$displayFrom_de = (isset($_REQUEST['NEWS_DISPLAY_FROM_DE'])) ? $_REQUEST['NEWS_DISPLAY_FROM_DE'] : null;
	$displayTo_de = (isset($_REQUEST['NEWS_DISPLAY_TO_DE'])) ? $_REQUEST['NEWS_DISPLAY_TO_DE'] : null;
	$newsId = (isset($_REQUEST['NEWS_ID'])) ? $_REQUEST['NEWS_ID'] : null;
	
	$response['NEWS_ID'] = $newsId;
	
	if ($newsId != null){
		$sql = 'select headline_de_id, headline_en_id, short_info_de_id, short_info_en_id, long_info_en_id, long_info_de_id from ' . DB_BIO . '.news_view where news_id = ?';
		$params = array($newsId);
		
		try{
			$sqlObj = Sql_Object::getInstance();
			$sqlObj->connect();
			$langObj = new Language($sqlObj);
			$sqlObj->beginTransaction();
			$sqlObj->execute($sql, $params);
				if (($row = $sqlObj->fetch()) != null){
					$headline_de_id = $row['headline_de_id'];
					$headline_en_id = $row['headline_en_id'];
					$langObj->updateValue($headline_de, $headline_de_id);
					$langObj->updateValue($headline_en, $headline_en_id);
					
					$shortinfo_de_id = $row['short_info_de_id'];
					$shortinfo_en_id = $row['short_info_en_id'];
					$langObj->updateValue($shortinfo_de, $shortinfo_de_id);
					$langObj->updateValue($shortinfo_en, $shortinfo_en_id);
					
					$longinfo_de_id = $row['long_info_de_id'];
					$longinfo_en_id = $row['long_info_en_id'];
					$langObj->updateValue($longinfo_de, $longinfo_de_id);
					$langObj->updateValue($longinfo_en, $longinfo_en_id);
				}
				
			$sql = 'update ' . DB_BIO . '.news set display_from = cast(? as date), display_to = cast(? as date) where news_id = ?';
			$params = array($displayFrom_de, $displayTo_de, $newsId);
			$sqlObj->execute($sql, $params);	
			$sqlObj->endTransaction();
			$sqlObj->disconnect();
		} catch (Exception $e){
			$response['SUCCESS'] = 0;
			$response['ERROR_MSG'] = $e->getMessage();
		}
	}

	return $response;
}


/**
 * creates a new news entry in bio.news
 */
function createNewNews(){
	$response = array('SUCCESS' => 1, 'SUCCESS_MSG' => 'News erfolgreich angelegt!', 'NEWS_ID' => null);
	$displayFrom_de = (isset($_REQUEST['NEWS_DISPLAY_FROM_DE'])) ? $_REQUEST['NEWS_DISPLAY_FROM_DE'] : null;
	$displayTo_de = (isset($_REQUEST['NEWS_DISPLAY_TO_DE'])) ? $_REQUEST['NEWS_DISPLAY_TO_DE'] : null;
	
	try{
	$sql = 'insert into ' . DB_BIO . '.news (display_from, display_to, creationdate) values (cast(? as date), cast(? as date), curdate())';
	$params = array($displayFrom_de, $displayTo_de);
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->beginTransaction();
	$sqlObj->execute($sql, $params);
	$newsId =  $sqlObj->getLastRecordId();
	
	$langObj = new Language($sqlObj);
	setHeadline($langObj, $sqlObj, $newsId, APPLICATION_ID);
	setShortInfo($langObj, $sqlObj, $newsId, APPLICATION_ID);
	setLongInfo($langObj, $sqlObj, $newsId, APPLICATION_ID);

	$sqlObj->endTransaction();
	$sqlObj->disconnect();
	
	$response['NEWS_ID'] = $newsId;
	} catch (Exception $e) {
		$response['SUCCESS'] = 0;
		$response['ERROR_MSG'] = $e->getMessage();
	}
	return $response;
}


/**
 * registrates a news language key for headline and sets german and english value
 * @param 		mixed		 $langObj
 * @param 		mixed		 $sqlObj
 * @param 		int			 $newsId
 * @param 		int			 $applicationId
 */
function setHeadline($langObj, $sqlObj, $newsId, $applicationId){
	$headline_de = (isset($_REQUEST['NEWS_HEADLINE_DE'])) ? $_REQUEST['NEWS_HEADLINE_DE'] : null;
	$headline_en = (isset($_REQUEST['NEWS_HEADLINE_EN'])) ? $_REQUEST['NEWS_HEADLINE_EN'] : null;
	
	$languageKeyId = $langObj->registrateKey('news_' . $newsId . '_headline', $applicationId);
	setAttribute('headline', $headline_de, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, GERMAN);
	setAttribute('headline', $headline_en, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, ENGLISH);
}


/**
 * registrates a news language key for long info and sets german and english value
 * @param 		mixed		 $langObj
 * @param 		mixed		 $sqlObj
 * @param 		int			 $newsId
 * @param 		int			 $applicationId
 */
function setLongInfo($langObj, $sqlObj, $newsId, $applicationId){
	$longInfo_de = (isset($_REQUEST['NEWS_LONG_INFO_DE'])) ? $_REQUEST['NEWS_LONG_INFO_DE'] : null;
	$longInfo_en = (isset($_REQUEST['NEWS_LONG_INFO_EN'])) ? $_REQUEST['NEWS_LONG_INFO_EN'] : null;
	
	$languageKeyId = $langObj->registrateKey('news_' . $newsId . '_long_info', $applicationId);
	setAttribute('long_info', $longInfo_de, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, GERMAN);
	setAttribute('long_info', $longInfo_en, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, ENGLISH);
}



/**
 * registrates a news language key for short info and sets german and english value
 * @param 		mixed		 $langObj
 * @param 		mixed		 $sqlObj
 * @param 		int			 $newsId
 * @param 		int			 $applicationId
 */
function setShortInfo($langObj, $sqlObj, $newsId, $applicationId){
	$shortInfo_de = (isset($_REQUEST['NEWS_SHORT_INFO_DE'])) ? $_REQUEST['NEWS_SHORT_INFO_DE'] : null;
	$shortInfo_en = (isset($_REQUEST['NEWS_SHORT_INFO_EN'])) ? $_REQUEST['NEWS_SHORT_INFO_EN'] : null;
	
	$languageKeyId = $langObj->registrateKey('news_' . $newsId . '_short_info', $applicationId);
	setAttribute('short_info', $shortInfo_de, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, GERMAN);
	setAttribute('short_info', $shortInfo_en, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, ENGLISH);
}


/**
 * registrates a new language value for given language and set entry in news table to langugage key
 * @param 		string		$column
 * @param 		string		$value
 * @param 		int			$newsId
 * @param 		mixed		$sqlObj
 * @param 		int			$applicationId
 * @param 		mixed		$langObj
 * @param 		int			$languageKeyId
 * @param 		string		$language
 */
function setAttribute($column, $value, $newsId, $sqlObj, $applicationId, $langObj, $languageKeyId, $language){
	$langObj->registrateValue($value, $languageKeyId, $language);
	
	$sql = 'update ' . DB_BIO . '.news set ' . $column . '_id = ? where news_id = ?';
	$params = array($languageKeyId, $newsId);
	$sqlObj->execute($sql, $params);
}
?>